#include "ktloginwgt.h"
#include "ktloginaccountwgt.h"
#include "ktloginsettingwgt.h"
#include "ktcarouselimagewgt.h"

#include <QPainter>
#include <QPalette>
#include <QFile>

#define LOGINWGT_WIDGET_BG 620
#define LOGINWGT_HEIGHT_BG 350
#define LOGINWGT_WIDGET 300
#define LOGINWGT_HEIGHT 350

KtLoginWgt::KtLoginWgt(QWidget *parent)
	: QWidget(parent)
	, ui(new Ui::KtLoginWidgetClass())
	, m_accountWgt(Q_NULLPTR)
	, m_settingWgt(Q_NULLPTR)
	, m_picWgt(Q_NULLPTR)
	, m_hBoxLayout(Q_NULLPTR)
	, m_bShowCarouselImage(false)
{
	ui->setupUi(this);
	m_picWgt = new KtCarouselImageWgt(this);
	m_loginWgt = new QWidget(this);
	m_accountWgt = new KtLoginAccountWgt(m_loginWgt);
	m_settingWgt = new KtLoginSettingWgt(m_loginWgt);
	m_hBoxLayout = new QHBoxLayout(this);
	m_hBoxLayout->addWidget(m_picWgt);
	m_hBoxLayout->addWidget(m_loginWgt);
	initStyle();
	initConnect();
}

KtLoginWgt::~KtLoginWgt()
{
	delete ui;
}

void KtLoginWgt::setCarouselImageVisible(bool bShow)
{
	m_bShowCarouselImage = bShow;
	m_picWgt->setVisible(bShow);
	setFixedSize(bShow ? LOGINWGT_WIDGET_BG : LOGINWGT_WIDGET, bShow ? LOGINWGT_HEIGHT_BG : LOGINWGT_HEIGHT);
}

bool KtLoginWgt::getCarouselImageVisible()
{
	return m_bShowCarouselImage;
}

void KtLoginWgt::setCarouselImage(const QPixmap& pixmap, bool bShow/* = true*/)
{
	m_picWgt->setCarouselImage(pixmap);
	setCarouselImageVisible(bShow);
}

void KtLoginWgt::setTitlePic(const QIcon& icon)
{
	setWindowIcon(icon);
}

void KtLoginWgt::setTitleText(const QString& text)
{
	setWindowTitle(text);
}

void KtLoginWgt::setLoginInfo(const KtLoginInfo& info)
{
	setAccountInfo(info.accountInfo);
	setSettingInfo(info.settingInfo);
}

KtLoginInfo KtLoginWgt::getLoginInfo()
{
	KtLoginInfo info;
	info.accountInfo = m_accountWgt->getLoginAccountInfo();
	info.settingInfo = m_settingWgt->getLoginSettingInfo();
	return info;
}

void KtLoginWgt::setLoginTitleText(const QString& text)
{
	m_accountWgt->setLoginTitleText(text);
}

void KtLoginWgt::setRemembCBoxVisible(bool bShow)
{
	m_accountWgt->setRemembCBoxVisible(bShow);
}

bool KtLoginWgt::remembCBoxVisible()
{
	return m_accountWgt->remembCBoxVisible();
}

void KtLoginWgt::setAutoLoginCBoxVisible(bool bShow)
{
	m_accountWgt->setAutoLoginCBoxVisible(bShow);
}

bool KtLoginWgt::autoLoginCBoxVisible()
{
	return m_accountWgt->autoLoginCBoxVisible();
}

void KtLoginWgt::setAccountInfo(KtLoginAccountInfo info)
{
	m_accountWgt->setLoginAccountInfo(info);
}

void KtLoginWgt::setSettingInfo(KtLoginSettingInfo info)
{
	m_settingWgt->setLoginSettingInfo(info);
}

void KtLoginWgt::settingBtnClickedSlt()
{
	setSettingWgtVisible(true);
	Q_EMIT settingBtnClickedSgl();
}

void KtLoginWgt::loginBtnClickedSlt()
{
	Q_EMIT loginBtnClickedSgl(getLoginInfo());
}

void KtLoginWgt::settingSureSlt(KtLoginSettingInfo info)
{
	setSettingWgtVisible(false);
	Q_EMIT settingInfoChanagedSgl(info);
}

void KtLoginWgt::settingCancelSlt()
{
	setSettingWgtVisible(false);
	// TODO emit
}

void KtLoginWgt::initStyle()
{
	m_settingWgt->setMaximumWidth(LOGINWGT_WIDGET);
	m_accountWgt->setMaximumWidth(LOGINWGT_WIDGET);
	m_hBoxLayout->setContentsMargins(0, 0, 0, 0);
	m_settingWgt->setObjectName("KtLoginSettingWgt");
	setObjectName("KtLoginWgt");
	QFile file(":/ktloginwidget/resource/qss/ktloginwidget.qss");
	file.open(QFile::ReadOnly);
	setStyleSheet(QLatin1String(file.readAll()));
	setSettingWgtVisible(false);
	setCarouselImageVisible(false);
}

void KtLoginWgt::initConnect()
{
	// Account
	connect(m_accountWgt, &KtLoginAccountWgt::settingBtnClickedSgl, this, &KtLoginWgt::settingBtnClickedSlt);
	connect(m_accountWgt, &KtLoginAccountWgt::loginBtnClickedSgl, this, &KtLoginWgt::loginBtnClickedSlt);

	// Setting
	connect(m_settingWgt, &KtLoginSettingWgt::sureBtnClickedSgl, this, &KtLoginWgt::settingSureSlt);
	connect(m_settingWgt, &KtLoginSettingWgt::cancelBtnClickedSgl, this, &KtLoginWgt::settingCancelSlt);
}

void KtLoginWgt::resizeEvent(QResizeEvent* event)
{
	QWidget::resizeEvent(event);
	int centWgtWidth = width();
	int centWgtHeight = height();
	m_accountWgt->setGeometry(0, 0, centWgtWidth, centWgtHeight);
	m_settingWgt->setGeometry(0, 0, centWgtWidth, centWgtHeight);
}

void KtLoginWgt::paintEvent(QPaintEvent* event)
{
	return QWidget::paintEvent(event);
}

void KtLoginWgt::setSettingWgtVisible(bool visible)
{
	m_accountWgt->setVisible(!visible);
	m_settingWgt->setVisible(visible);
}
